package com.pengtu.gsj.listener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.pengtu.gsj.entity.app.Code;
import com.pengtu.gsj.entity.app.Organization;
import com.pengtu.gsj.entity.base.LabelValue;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.LabelHelper;

/**
 * <p>
 * StartupListener class used to initialize and database settings and populate
 * any application-wide drop-downs.
 * 
 * <p>
 * Keep in mind that this listener is executed outside of
 * OpenSessionInViewFilter, so if you're using Hibernate you'll have to
 * explicitly initialize all loaded data at the Dao or service level to avoid
 * LazyInitializationException. Hibernate.initialize() works well for doing
 * this.
 * 
 * @author
 */
public class StartupListener extends ContextLoaderListener implements
		ServletContextListener {

	private static final Log log = LogFactory.getLog(StartupListener.class);

	public void contextInitialized(ServletContextEvent event) {
		if (log.isDebugEnabled()) {
			log.debug("initializing context...");
		}
		super.contextInitialized(event);
		ServletContext context = event.getServletContext();
		setupContext(context);
	
	}
	public static void setupContext(ServletContext context) {
		// 码表: 
		setupCodeListContext(context);
		
	}
	public static void setupCodeListContext(ServletContext context){
		int i=0;
        ApplicationContext ctx = 
            WebApplicationContextUtils.getRequiredWebApplicationContext(context);
         SystemManageService systemManageService = (SystemManageService) ctx.getBean("systemManagerServiceImpl");
 		 List<Code> codesList =systemManageService.getAvailableCodeList();
 		 List<Organization> orgList=systemManageService.getAllJu();
 		 Map<Integer,List<LabelValue>>  categoryMap= new  HashMap<Integer,List<LabelValue>>();
		 List<LabelValue> lavelValues = new ArrayList<LabelValue>();
			int falgCategory = 0;
 		 for (Code appCode : codesList) {
 			 i++;
 			Integer category = appCode.getCategory();
 			if(falgCategory !=  category) {
 				categoryMap.put(falgCategory, lavelValues);	
 				falgCategory = category;	
 				lavelValues = new ArrayList<LabelValue>();
 			} 			
 	 			LabelValue lavelValue = new LabelValue(appCode.getCodeName(),appCode.getCodeId(),appCode.getCategoryDesc());
 	 			lavelValues.add(lavelValue);				
 		 }
 		 if(codesList.size()==i){
 			categoryMap.put(falgCategory,lavelValues);
 		 }
 		 if(orgList.size()>0){
 			 List<LabelValue> lab = new ArrayList<LabelValue>();
         for (Organization org : orgList) {
			LabelValue labelValue=new LabelValue(org.getOrgName(), org.getOrgNum(),org.getId());
        	lab.add(labelValue);
		 }
         categoryMap.put(8888, lab);
 		 }
 		 LabelHelper.buildLabelHelper(categoryMap);
	}
	
}
